<template>
  <div class="container">
    <div class="header">
      <!-- <div class="back-btn" @click="handleBack">
        <i class="fas fa-arrow-left"></i>
      </div> -->
      <div class="header-title">危机个案</div>
      <!-- <div class="back-btn" style="visibility: hidden">
        <i class="fas fa-arrow-left"></i>
      </div> -->
    </div>

    <!-- Loading 效果 -->
    <div v-if="loading" class="loading-overlay">
      <div class="loading-spinner"></div>
      <div class="loading-text">加载中...</div>
    </div>

    <div class="content">
      <!-- 搜索和筛选区域 -->
      <div class="search-section">
        <div class="search-container">
          <div class="search-input-wrapper">
            <i class="fas fa-search"></i>
            <input
              type="text"
              placeholder="搜索个案ID、客户姓名..."
              v-model="searchKeyword"
              @input="handleSearch"
            />
          </div>
          <div class="filter-buttons">
            <span class="filter-button active" @click="handleFilter('all')">
              <i class="fas fa-list"></i>
              全部
            </span>
            <span class="filter-button" @click="handleFilter('high')">
              <i class="fas fa-exclamation-triangle"></i>
              高风险
            </span>
            <span class="filter-button" @click="handleFilter('progress')">
              <i class="fas fa-spinner"></i>
              进行中
            </span>
            <span class="filter-button" @click="handleFilter('new')">
              <i class="fas fa-check-circle"></i>
              新个案
            </span>
          </div>
        </div>
      </div>

      <!-- 个案卡片网格 -->
      <div class="cards-grid">
        <!-- 动态渲染个案卡片 -->
        <div
          v-for="caseItem in filteredCaseList"
          :key="caseItem.id"
          class="case-card"
          :class="getCardClass(caseItem)"
        >
          <div class="card-header">
            <span class="case-id">{{ caseItem.code }}</span>
            <span class="status-badge">{{ caseItem.crisisLevel }}</span>
          </div>
          <div class="card-body">
            <h3>{{ caseItem.zkName }}</h3>
            <p class="client-info">
              {{ getGenderText(caseItem.gender) }}，{{ caseItem.age || 0 }}岁，
              {{ caseItem.memberno }}，{{ caseItem.counselDate }}
            </p>

            <div class="info-list">
              <div class="info-item">
                <span class="info-label">项目经理</span>
                <span class="info-value">{{ caseItem.pmName }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">客户公司</span>
                <span class="info-value">{{ caseItem.companyName }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">咨询主题</span>
                <span class="info-value">{{
                  caseItem.presentProblemValue
                }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">紧急联系人</span>
                <span class="info-value">
                  {{ caseItem.emergencyContact }}
                  {{
                    caseItem.emergencyPhone
                      ? `(${caseItem.emergencyPhone})`
                      : ""
                  }}
                </span>
              </div>
              <div class="info-item">
                <span class="info-label">保密情况</span>
                <span class="info-value">{{
                  caseItem.confidentSituation
                }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">危机状态</span>
                <span class="info-value">{{ caseItem.crisiSituation }}</span>
              </div>
            </div>

            <div class="progress-info">
              <span>进度:</span>
              <span>{{ caseItem.progress }}%</span>
            </div>
            <div class="progress-bar">
              <div
                class="progress-fill"
                :style="{ width: caseItem.progress + '%' }"
              ></div>
            </div>
          </div>
          <div class="card-footer">
            <div class="counselor-info">
              <div class="avatar">{{ getAvatarText(caseItem.ucName) }}</div>
              <div>
                <p class="counselor-name">{{ caseItem.ucName }}</p>
                <p class="counselor-title">项目经理</p>
              </div>
            </div>
            <div class="action-buttons">
              <button
                class="action-button"
                @click="handleViewClick(caseItem.id)"
              >
                <i class="fas fa-eye"></i>
              </button>
              <button
                class="action-button"
                @click="save(caseItem.id)"
                v-if="caseItem.urgenNeeds == 4"
              >
                <i class="fas fa-edit"></i>
              </button>
              <button class="action-button" @click="PracticeList(caseItem)">
                <i class="fas fa-chart-line"></i>
              </button>
            </div>
          </div>
        </div>

        <!-- 空状态提示 -->
        <div
          v-if="filteredCaseList.length === 0 && !loading"
          class="empty-state"
        >
          <i class="fas fa-inbox"></i>
          <p>暂无个案数据</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getManagerCaseList } from "../../api/case";

export default {
  name: "CaseManagement",
  data() {
    return {
      loading: false,
      caseList: [],
      searchKeyword: "",
      currentFilter: "all",
      filterButtons: [],
    };
  },
  computed: {
    filteredCaseList() {
      let filtered = this.caseList;

      // 搜索过滤
      if (this.searchKeyword) {
        const keyword = this.searchKeyword.toLowerCase();
        filtered = filtered.filter(
          (item) =>
            item.code.toLowerCase().includes(keyword) ||
            item.ucName.toLowerCase().includes(keyword) ||
            item.companyName.toLowerCase().includes(keyword)
        );
      }

      // 筛选过滤
      switch (this.currentFilter) {
        case "high":
          filtered = filtered.filter((item) => item.crisisLevel === "高风险");
          break;
        case "progress":
          filtered = filtered.filter(
            (item) =>
              item.crisisLevel !== "新个案" &&
              item.crisisLevel !== "已结案" &&
              parseInt(item.progress) > 0 &&
              parseInt(item.progress) < 100
          );
          break;
        case "new":
          filtered = filtered.filter((item) => item.crisisLevel === "新个案");
          break;
        default:
          // 全部个案，不过滤
          break;
      }

      return filtered;
    },
  },
  mounted() {
    this.loadCaseList();
    this.initFilterButtons();
    this.initCardHoverEffects();
  },
  created() {
    this.teacherId = this.$route.query.teacherId;
    localStorage.setItem("teacherId", this.teacherId);
  },
  methods: {
    async loadCaseList() {
      this.loading = true;
      try {
        const data = {
          teacherId: this.teacherId,
        };
        const res = await getManagerCaseList(data);
        if (res.code === 200) {
          this.caseList = res.data.records || [];
        } else {
          console.error("获取个案列表失败:", res.message);
          this.caseList = [];
        }
      } catch (error) {
        console.error("获取个案列表异常:", error);
        this.caseList = [];
      } finally {
        this.loading = false;
      }
    },

    handleSearch() {
      // 搜索功能已通过计算属性实现
    },

    handleFilter(filterType) {
      this.currentFilter = filterType;
      // 更新按钮激活状态
      this.filterButtons.forEach((button) => {
        button.classList.remove("active");
      });
      this.filterButtons[this.getFilterButtonIndex(filterType)].classList.add(
        "active"
      );
    },

    getFilterButtonIndex(filterType) {
      const filterMap = { all: 0, high: 1, progress: 2, new: 3 };
      return filterMap[filterType] || 0;
    },

    getCardClass(caseItem) {
      const crisisLevel = caseItem.crisisLevel;
      if (crisisLevel === "高风险") return "high-risk";
      if (crisisLevel === "新个案") return "new-case";
      if (crisisLevel === "低风险") return "in-progress";
      return "closed-case";
    },

    getGenderText(gender) {
      if (gender === "保密") return "保密";
      if (gender === "男") return "男性";
      if (gender === "女") return "女性";
      return "未填写";
    },

    getAvatarText(name) {
      return name ? name.charAt(0) : "未";
    },

    handleViewClick(id) {
      this.$router.push({
        path: "/case/wjgack",
        query: { id: id },
      });
    },
    save(id) {
      this.$router.push({
        path: "/case/wjgatx",
        query: { id: id },
      });
    },
    PracticeList(caseItem) {
      this.$router.push({
        path: "/case/PracticeList",
        query: { caseItem: JSON.stringify(caseItem) },
      });
    },

    handleBack() {
      this.$router.back();
    },

    initFilterButtons() {
      this.$nextTick(() => {
        this.filterButtons = document.querySelectorAll(".filter-button");
      });
    },

    initCardHoverEffects() {
      this.$nextTick(() => {
        const cards = document.querySelectorAll(".case-card");
        cards.forEach((card) => {
          card.addEventListener("mouseenter", function () {
            this.style.boxShadow =
              "0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04)";
            this.style.transform = "translateY(-5px)";
          });
          card.addEventListener("mouseleave", function () {
            this.style.boxShadow =
              "0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06)";
            this.style.transform = "translateY(0)";
          });
        });
      });
    },
  },
};
</script>

<style scoped>
@import url("https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap");
@import url("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css");

.container {
  font-family: "Noto Sans SC", sans-serif;
  background-color: #f8fafc;
  color: #1e293b;
  line-height: 1.6;
  min-height: 100vh;
  position: relative;
}
.content {
  padding: 10px 16px;
  box-sizing: border-box;
}

@media (min-width: 640px) {
  .container {
    padding: 32px 24px;
  }
}

@media (min-width: 1024px) {
  .container {
    padding: 32px 32px;
  }
}

.container > div {
  max-width: 1280px;
  margin-left: auto;
  margin-right: auto;
}

/* Loading 样式 */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

.loading-text {
  font-size: 16px;
  color: #666;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 空状态样式 */
.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 60px 20px;
  color: #9ca3af;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state p {
  font-size: 16px;
  margin: 0;
}

/* 标题区域样式 */

.title-section {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
}

@media (min-width: 768px) {
  .title-section {
    margin-bottom: 0;
  }
}

.icon-wrapper {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  background-color: #dbeafe;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
}

.icon-wrapper i {
  color: #2563eb;
  font-size: 24px;
}

.title-section h1 {
  font-size: 24px;
  font-weight: 700;
  color: #374151;
}

.title-section p {
  color: #6b7280;
  margin-top: 4px;
}

.button-group {
  display: flex;
  gap: 12px;
}

/* 搜索区域样式 */
.search-section {
  background-color: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
  margin-bottom: 32px;
}

.search-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

@media (min-width: 768px) {
  .search-container {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
}

.search-input-wrapper {
  position: relative;
  flex: 1;
  max-width: 448px;
}

.search-input-wrapper i {
  position: absolute;
  top: 50%;
  left: 12px;
  transform: translateY(-50%);
  color: #9ca3af;
}

.search-input-wrapper input {
  padding-left: 40px;
  padding-right: 16px;
  padding-top: 8px;
  padding-bottom: 8px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  width: 100%;
  font-size: 14px;
  box-sizing: border-box;
}

.search-input-wrapper input:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.5);
}

.filter-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.filter-button {
  padding: 8px 16px;
  background-color: #f3f4f6;
  color: #374151;
  border-radius: 9999px;
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.2s ease;
}

.filter-button.active {
  background-color: #dbeafe;
  color: #1e40af;
}

.filter-button:hover {
  background-color: #e5e7eb;
}

.filter-button.active:hover {
  background-color: #c7d2fe;
}

.filter-button i {
  margin-right: 8px;
}

/* 卡片网格样式 */
.cards-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 24px;
}

@media (min-width: 768px) {
  .cards-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (min-width: 1024px) {
  .cards-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* 卡片通用样式 */
.case-card {
  background-color: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
    0 2px 4px -1px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
}

.card-header {
  padding: 20px;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.case-id {
  font-size: 14px;
  color: #6b7280;
  font-weight: 500;
}

.status-badge {
  padding: 4px 12px;
  border-radius: 9999px;
  font-size: 12px;
  font-weight: 500;
}

.high-risk .status-badge {
  background-color: #fee2e2;
  color: #991b1b;
}

.in-progress .status-badge {
  background-color: #fef3c7;
  color: #92400e;
}

.new-case .status-badge {
  background-color: #dbeafe;
  color: #1e40af;
}

.closed-case .status-badge {
  background-color: #d1fae5;
  color: #065f46;
}

.card-body {
  padding: 20px;
}

.card-body h3 {
  font-size: 20px;
  font-weight: 600;
  color: #374151;
}

.client-info {
  font-size: 14px;
  color: #6b7280;
  margin-top: 4px;
}

.info-list {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.info-item {
  display: flex;
}

.info-label {
  font-size: 14px;
  color: #6b7280;
  width: 96px;
  flex-shrink: 0;
}

.info-value {
  font-size: 14px;
  font-weight: 500;
  flex: 1;
}

.progress-info {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid #e5e7eb;
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #6b7280;
}

.progress-bar {
  height: 8px;
  border-radius: 4px;
  background-color: #e5e7eb;
  margin-top: 8px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.3s ease;
}

.high-risk .progress-fill {
  background-color: #ef4444;
}

.in-progress .progress-fill {
  background-color: #eab308;
}

.new-case .progress-fill {
  background-color: #3b82f6;
}

.closed-case .progress-fill {
  background-color: #10b981;
}

.card-footer {
  background-color: #f9fafb;
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.counselor-info {
  display: flex;
  align-items: center;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 9999px;
  background-color: #2563eb;
  color: white;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
}

.counselor-name {
  font-size: 14px;
  font-weight: 500;
}

.counselor-title {
  font-size: 12px;
  color: #6b7280;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.action-button {
  width: 36px;
  height: 36px;
  border-radius: 9999px;
  background-color: white;
  border: 1px solid #d1d5db;
  color: #4b5563;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.2s;
}

.action-button:hover {
  background-color: #f3f4f6;
  border-color: #9ca3af;
}
.header {
  background: white;
  padding: 15px 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
  top: 0;
  z-index: 100;
}

.header-title {
  font-size: 1.3rem;
  font-weight: 600;
  color: #1a233d;
}

.back-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #eef0ff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #5b67ff;
  cursor: pointer;
}
</style>
